package org.jeecg.crawler.task;


import lombok.SneakyThrows;
import org.jeecg.common.system.vo.DictModel;
import org.jeecg.common.util.DateTimeUtil;
import org.jeecg.crawler.content.ContentNewsCrawler;
import org.jeecg.crawler.node.BaseNewsCrawler;
import org.jeecg.crawler.node.DetailAutoNewsCrawler;
import org.jeecg.modules.crawlerpaper.entity.CrawlerInfo;
import org.jeecg.modules.crawlerpaper.service.ICrawlerInfoService;
import org.jeecg.modules.system.service.ISysDictService;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.util.CollectionUtils;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Configuration      //1.主要用于标记配置类，兼备Component的效果。
@EnableScheduling   // 2.开启定时任务
public class NodeScheduleTask {
    @Autowired
    private ICrawlerInfoService crawlerInfoService;

    @Autowired
    private ISysDictService sysDictService;

    //参数：baseurl,mainUrl,dataurl,page,scoureName,articleTime;

    @SneakyThrows
    @Scheduled(cron = "0 19 9 * * ?")
    public void nanjingchenbao() throws JobExecutionException {
           String  parameter = "http://njcb.xhby.net/pc,/layout,0,/node_A01.html,南京晨报,0";

            String[] params = parameter.split(",");
            if(params[2].equals("0")){
                params[2] = "/"+ DateTimeUtil.getTodayChar9();

            }
            if(params[5].equals("0")){
                params[5] = DateTimeUtil.getTodayChar8En();
            }
            this.getCrewlerInfo(params[0],params[1],params[2],params[3],params[4],params[5]);



    }


    @SneakyThrows
    @Scheduled(cron = "0 20 9 * * ?")
    public void xinhuaribao() throws JobExecutionException {
        String  parameter = "http://xh.xhby.net/pc,/layout,0,/node_1.html,新华日报,0";

        String[] params = parameter.split(",");
        if(params[2].equals("0")){
            params[2] = "/"+ DateTimeUtil.getTodayChar9();

        }
        if(params[5].equals("0")){
            params[5] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0],params[1],params[2],params[3],params[4],params[5]);



    }


    @SneakyThrows
    @Scheduled(cron = "0 21 9 * * ?")
    public void jiangsufazhibao()  {
        String  parameter = "http://jsfzb.xhby.net/pc,/layout,0,/node_A01.html,江苏法制报,0";

        String[] params = parameter.split(",");
        if(params[2].equals("0")){
            params[2] = "/"+ DateTimeUtil.getTodayChar9();

        }
        if(params[5].equals("0")){
            params[5] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0],params[1],params[2],params[3],params[4],params[5]);



    }
    @SneakyThrows
    @Scheduled(cron = "0 22 9 * * ?")
    public void jiangsujingjibao()  {
        String  parameter = "http://jsjjb.xhby.net/pc,/layout,0,/node_A01.html,江苏经济报,0";

        String[] params = parameter.split(",");
        if(params[2].equals("0")){
            params[2] = "/"+ DateTimeUtil.getTodayChar9();

        }
        if(params[5].equals("0")){
            params[5] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0],params[1],params[2],params[3],params[4],params[5]);



    }

    @SneakyThrows
    @Scheduled(cron = "0 23 9 * * ?")
    public void yangziwanbao()  {
        String  parameter = "http://epaper.yzwb.net/pc,/layout,0,/node_A01.html,杨子晚报,0";

        String[] params = parameter.split(",");
        if(params[2].equals("0")){
            params[2] = "/"+ DateTimeUtil.getTodayChar9();

        }
        if(params[5].equals("0")){
            params[5] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0],params[1],params[2],params[3],params[4],params[5]);



    }

    @SneakyThrows
    @Scheduled(cron = "0 24 9 * * ?")
    public void jinagnanshibao()  {
        String  parameter = "http://jnsb.xhby.net/pc,/layout,0,/node_A01.html,江南时报,0";

        String[] params = parameter.split(",");
        if(params[2].equals("0")){
            params[2] = "/"+ DateTimeUtil.getTodayChar9();

        }
        if(params[5].equals("0")){
            params[5] = DateTimeUtil.getTodayChar8En();
        }
        this.getCrewlerInfo(params[0],params[1],params[2],params[3],params[4],params[5]);



    }



    public void  getCrewlerInfo(String baseUrl, String mainUrl,String dateUrl,String page,String sourceName,String articleTime) throws Exception {
        try {

            ContentNewsCrawler.lock.lock();
            BaseNewsCrawler crawler = new BaseNewsCrawler("crawl", true,dateUrl,baseUrl,mainUrl,page);
            crawler.start(1);
            Map<String, String> urlMap = crawler.getUrlMap();

            DetailAutoNewsCrawler crawlerDetail = new DetailAutoNewsCrawler("crawl", true,urlMap,baseUrl,sourceName,articleTime);
            crawlerDetail.start(1);
            List<CrawlerInfo> crawlerInfoList = crawlerDetail.getCrawlerInfoList();
            if(!CollectionUtils.isEmpty(crawlerInfoList)){
                List<DictModel> keywords = sysDictService.queryDictItemsByCode("keywords");
                if(!CollectionUtils.isEmpty(keywords)){

                    Iterator<CrawlerInfo> iterator = crawlerInfoList.iterator();
                    while (iterator.hasNext()){
                        CrawlerInfo next = iterator.next();
                        Boolean removeFlag = true;
                        for (DictModel keyword : keywords) {
                            if(next.getArticleName().contains(keyword.getValue())||next.getArticleContent().toString().contains(keyword.getValue())){
                                removeFlag = false;
                                break;
                            }
                        }
                        if(removeFlag){
                            iterator.remove();
                        }

                    }
                }
                if(!CollectionUtils.isEmpty(crawlerInfoList)){
                    crawlerInfoService.saveOrUpdateBatch(crawlerInfoList);

                }

            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            ContentNewsCrawler.lock.unlock();
        }
    }
}
